package handler

import (
	"github.com/gin-gonic/gin"
	"monthly-test/api/request"
	"monthly-test/consts"
	"monthly-test/internal/dao"
	"monthly-test/internal/model"
	"monthly-test/pkg"
	"net/http"
)

func Register(c *gin.Context) {

}

func Login(c *gin.Context) {
	var req request.LoginReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": err.Error(),
		})
		return
	}
	var user model.User

	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"date": nil,
		})
		return
	}
	dao.GetOneById(user.Id, &model.User{
		Username: req.Username,
		Password: req.Password,
	})
	token, _ := pkg.NewJWT(consts.JWT_SELECT_KEY).CreateToken(pkg.CustomClaims{
		ID: user.Id,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"date": token,
	})
}
